package com.kaifamiao.month04.day26;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Demo02 {

    private static final List<String> TYPES = List.of(".jpg", ".jpeg", ".png", ".gif", ".svg", ".bmp");

    public static void main(String[] args) {
        File file = new File("E:\\3 图片\\背景");
        ArrayList<File> fileArrayList = new ArrayList<>(Arrays.asList(file.listFiles()));
        for (int i = 0; i < fileArrayList.size(); i++) {
            File f = fileArrayList.get(i);
            if (!f.isFile()) {
                File[] fs = f.listFiles();
                if (fs == null )  { // 文件夹为空
                    fileArrayList.remove(i);
                    i--;
                }  else {
                    fileArrayList.addAll(Arrays.asList());
                    fileArrayList.remove(i);
                    i--;
                }
            } else {
                if (!TYPES.contains(getEnd(f.getName()).toLowerCase())) {
                    fileArrayList.remove(i);
                    i--;
                }
            }
        }
        for (File f: fileArrayList) {
            System.out.println(f);
        }
//        File file = new File("E:\\3 图片\\背景\\effects");
//        System.out.println(file.isFile());
    }

    private static String getEnd(String str) {
        for (int i = str.length() - 1; i >= 0; i--) {
            if (str.charAt(i) == '.') {
                return str.substring(i);
            }
        }
        return str;
    }
}
